﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XGameFrame.Controls
{
    public delegate void OnClickHandler(object o, ClickEventArgs e);
    public class Button:Control
    {

        #region Events
        public event OnClickHandler ClickEvent;
        #endregion

        #region Fields
        Texture2D buttonPic;
        Texture2D hoveredButtonPic;
        #endregion

        #region Propertys

        public Texture2D ButtonPic
        {
            get { return buttonPic; }
            set { buttonPic = value; }
        }
        public Texture2D HoveredButtonPic
        {
            get { return hoveredButtonPic; }
            set { hoveredButtonPic = value; }
        }
        #endregion

        #region Constructor

        public Button(Rectangle pRectangle,Texture2D pPicForButton,Texture2D pHoveredPic)
            : base(pPicForButton,new Vector2(pRectangle.X,pRectangle.Y))
        {
            DestRectangle = pRectangle;
            SourceRectangle = new Rectangle(0, 0, pPicForButton.Width, pPicForButton.Height);
            Bounds = new Rectangle((int)Position.X, (int)Position.Y, pPicForButton.Width, pPicForButton.Height);
            ButtonPic = pPicForButton;
            HoveredButtonPic = pHoveredPic;
        }

        public Button(Vector2 pPosition, Texture2D pPicForButton, Texture2D pHoveredPic)
            : base(pPicForButton,pPosition)
        {
            Position = pPosition;
            DestRectangle = new Rectangle((int)Position.X, (int)Position.Y, pPicForButton.Width, pPicForButton.Height);
            SourceRectangle = new Rectangle(0, 0, pPicForButton.Width, pPicForButton.Height);
            Bounds = new Rectangle((int)Position.X, (int)Position.Y, pPicForButton.Width, pPicForButton.Height);
            ButtonPic = pPicForButton;
            HoveredButtonPic = pHoveredPic;
        }

        #endregion

        #region Abstract Methods

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (IsHover)
                spriteBatch.Draw(HoveredButtonPic, Position, SourceRectangle, Color.White, Rotation, Origin,Scale, SpriteEffect, LayerDepth);
            if (IsHover==false)
                spriteBatch.Draw(ButtonPic, Position, SourceRectangle, Color.White, Rotation, Origin,Scale, SpriteEffect, LayerDepth);
        }

        public override void HandleInput(PlayerIndex playerIndex)
        {
            

        }

        public override void OnHover(PlayerIndex playerIndex)
        {
            if (IsHover && InputHandler.MouseButtonLeftPressed())
            {
                ClickEventArgs e = new ClickEventArgs(Convert.ToString(this.Value));
            }
            
        }
        #endregion

        #region event Method


        public void throwClickEvent(ClickEventArgs e)
        {
            if (ClickEvent != null)
                ClickEvent(this, e);
        }

        #endregion


        #region Destructor
        ~Button()
        {
        }

        #endregion



    }
}
